<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

class vm_ps_search_pos {
    function getRestaurantList($vendor_id = 0, $location = '', $hungry = '') {
        $arrList = array();
        
        $db = new ps_DB;
        
        $q_hungry = "SELECT v_c.vendor_id, v_c_t.name as cuisine_name 
                     FROM #__{vm}_vendor_cuisine v_c
                        LEFT JOIN #__{vm}_cuisine_type v_c_t ON v_c.cuisine_id = v_c_t.id";
        if ($hungry != '') {
            $q_hungry .= " AND v_c_t.name LIKE '%".$hungry."%'";
        }
        
        $q = "SELECT v.vendor_id, v.vendor_name, v.vendor_keyword, 
                    v_l.vendor_delivery, v_l.vendor_address_1, v_l.vendor_address_2, 
                    v_l.vendor_city, v_l.vendor_state,v_l.vendor_phone,v_l.vendor_zip,  
                    tmp_v_c.cuisine_name
              FROM #__{vm}_vendor v
                INNER JOIN #__{vm}_vendor_locations v_l ON v.vendor_id = v_l.vendor_id
                LEFT JOIN (".$q_hungry.") tmp_v_c ON v.vendor_id = tmp_v_c.vendor_id
              WHERE 1=1";
        
        if ($vendor_id != 0) {
            $q .= " AND v.vendor_id IN (".$vendor_id.")";
        }
        
        if ($location != '') {
            $q .= " AND ( 
                            v.vendor_name LIKE '%".$location."%' 
                        OR
                            v_l.vendor_address_1 LIKE '%".$location."%'
                        OR 
                            v_l.vendor_address_2 LIKE '%".$location."%'
                        OR
                            v_l.vendor_city LIKE '%".$location."%'
                        OR
                            v_l.vendor_state LIKE '%".$location."%'
                        OR
                            v_l.vendor_zip LIKE '%".$location."%'
                        )";
        }
                
        $q .= " ORDER BY v.vendor_name, tmp_v_c.cuisine_name";
        
        //print($q);
        
        $db->query($q);  
        
        $db->next_record();
        
        $cuisine_name = "";
        $vendor_name_old = "";
        $index = 0;
        $rowCount = count($db->record);
        $arrVendors = array();
        
        $script_address = '_GoogleMaps_.arrAddress = new Object();';
        $index_address = 0;
        for ($i = 0; $i < $rowCount; $i++) {
            $obj = $db->record[$i];
            $cuisine_name .= $obj->cuisine_name.", ";
            
            if ( ($i == ($rowCount - 1)) || ($db->record[$i]->vendor_name != $db->record[$i + 1]->vendor_name) ) {
                $arrVendors[$index]["vendor_id"] = (int)$obj->vendor_id;
                $arrVendors[$index]["vendor_name"] = $obj->vendor_name;
                $arrVendors[$index]["vendor_keyword"] = $obj->vendor_keyword;
                
                $arrVendors[$index]["vendor_address_1"] = $obj->vendor_address_1;
                $arrVendors[$index]["vendor_address_2"] = $obj->vendor_address_2;
                $arrVendors[$index]["vendor_city"] = $obj->vendor_city;
                $arrVendors[$index]["vendor_state"] = $obj->vendor_state;
                $arrVendors[$index]["vendor_zip"] = $obj->vendor_zip;
                $arrVendors[$index]["vendor_phone"] = $obj->vendor_phone;
                
                $arrVendors[$index]["vendor_delivery"] = ((int)$obj->vendor_delivery == 1) ? "YES" : "NO";
                
                if (strlen($cuisine_name) > 0) {
                    $cuisine_name = substr($cuisine_name, 0, strlen($cuisine_name) - 2);
                }
                
                $arrVendors[$index]["vendor_cuisine"] = $cuisine_name;
                
                $address = $obj->vendor_address_1.', '.$obj->vendor_city.', '.$obj->vendor_state;
                $script_address .= '_GoogleMaps_.arrAddress['.$index_address.'] = CreateNewAddress("'.addslashes($address).'");';
                $index_address++;
                if ($obj->vendor_address_2 != "") {
                    $address = $obj->vendor_address_2.', '.$obj->vendor_city.', '.$obj->vendor_state;
                    $script_address .= '_GoogleMaps_.arrAddress['.$index_address.'] = CreateNewAddress("'.addslashes($address).'");';
                    $index_address++;
                }
                
                $cuisine_name = "";
                $index++;
            }
            
            $vendor_name_old = $obj->vendor_name;
        }
        
        $arrList['vendors'] = $arrVendors;
        $arrList['address'] = $script_address;
        
        return $arrList;
    }
    
    function getCategoryId($vendor_id) {
        $q = "SELECT v_p_c_x.category_id
                FROM #__{vm}_product v_p
                    INNER JOIN #__{vm}_product_category_xref v_p_c_x ON v_p.product_id = v_p_c_x.product_id
                    INNER JOIN #__{vm}_category_xref v_c_x ON v_p_c_x.category_id = v_c_x.category_child_id AND v_c_x.category_parent_id = 3
                WHERE v_p.vendor_id = ".$vendor_id."
                GROUP BY v_p_c_x.category_id";
                
        $db = new ps_DB;
        $db->query($q);  
        $db->next_record();
        
        $category_id = '';
        for ($i = 0; $i < count($db->record); $i++) {
            $obj = $db->record[$i];
            $category_id .=  $obj->category_id.',';
        }
        
        if (strlen($category_id) > 0) {
            $category_id = substr($category_id, 0, strlen($category_id) - 1);
        }
        
        return $category_id;
    }
}

// Check if there is an extended class in the Themes and if it is allowed to use them
// If the class is called outside Virtuemart, we have to make sure to load the settings
// Thomas Kahl - Feb. 2009
if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
    include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
    // Load the theme-user_class as extended
    include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
    // Otherwise we have to use the original classname to extend the core-class
    class ps_search_pos extends vm_ps_search_pos {}
}
?>
